package com.lly.songhigh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.activity.ChartsDetailActivity;
import com.lly.songhigh.activity.LocalMusicListActivity;
import com.lly.songhigh.activity.MyCollectionActivity;
import com.lly.songhigh.activity.RecentPlayActivity;
import com.lly.songhigh.activity.SongSheetDetailActivity;
import com.lly.songhigh.adapter.CollectionSongListAdapter;
import com.lly.songhigh.entity.dao.AddTo;
import com.lly.songhigh.greendao.DaoSingleton;
import com.lly.songhigh.utils.SharedPreferencesUtils;
import com.lly.songhigh.view.NoScrollListView;

import java.util.List;

/**
 * Created by dllo on 15/10/20.
 */
public class MineFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {

    private LinearLayout mine_local_music;
    private LinearLayout mine_recently_play;
    private LinearLayout mine_dld_manager;
    private LinearLayout mine_my_collection;
    private TextView mine_local_music_count;
    private TextView mine_recently_music_count;
    private TextView create_song_sheet_count;
    private TextView mine_singer_count;
    private NoScrollListView collectSongList;
    private DaoSingleton daoSingleton;
    private List<AddTo> addToList;
    private CollectionSongListAdapter adapter;
    private static final String TAG = "MineFragment";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mine_local_music = (LinearLayout) view.findViewById(R.id.mine_local_music);
        mine_recently_play = (LinearLayout) view.findViewById(R.id.mine_recently_play);
        mine_my_collection = (LinearLayout) view.findViewById(R.id.mine_my_collection);
        mine_local_music_count = (TextView) view.findViewById(R.id.mine_local_music_count);
        mine_recently_music_count = (TextView) view.findViewById(R.id.mine_recently_music_count);
        mine_singer_count = (TextView) view.findViewById(R.id.mine_collection_count);
        collectSongList = (NoScrollListView) view.findViewById(R.id.collect_song_list);
        create_song_sheet_count = (TextView) view.findViewById(R.id.create_song_sheet_count);

        collectSongList.setOnItemClickListener(this);
        mine_local_music.setOnClickListener(this);
        mine_my_collection.setOnClickListener(this);
        mine_recently_play.setOnClickListener(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        daoSingleton = DaoSingleton.getInstance();
        addToList = daoSingleton.getAddTo();
        adapter = new CollectionSongListAdapter(getActivity(), addToList);
        collectSongList.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        int count = SharedPreferencesUtils.getSharePreInt(getActivity(), "local_music", "music_count");
        mine_local_music_count.setText("（" + String.valueOf(count) + "）");
        int collectionCount = SharedPreferencesUtils.getSharePreInt(getActivity(), "collection", "count");
        mine_singer_count.setText("（" + String.valueOf(collectionCount) + "）");
        int songListCount = daoSingleton.getAddTo().size();
        addToList = daoSingleton.getAddTo();
        adapter.refreshData(addToList);
        create_song_sheet_count.setText("（" + String.valueOf(songListCount) + "）");
        int recent_play_count = SharedPreferencesUtils.getSharePreInt(getActivity(), "RecentPlay", "count");
        mine_recently_music_count.setText("（" + String.valueOf(recent_play_count) + "）");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mine_local_music:
                Intent intentToLocal = new Intent(getActivity(), LocalMusicListActivity.class);
                startActivity(intentToLocal);
                break;
            case R.id.mine_recently_play:
                Intent intentToRecent = new Intent(getActivity(), RecentPlayActivity.class);
                startActivity(intentToRecent);
                break;
            case R.id.mine_my_collection:
                Intent intentToCollection = new Intent(getActivity(), MyCollectionActivity.class);
                startActivity(intentToCollection);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        AddTo addTo = (AddTo) parent.getItemAtPosition(position);
        String resouce = addTo.getResource();
        if (resouce.equals("排行榜")) {
            Intent intent = new Intent(getActivity(), ChartsDetailActivity.class);
            intent.putExtra("ranklist_id", addTo.getSongListId());
            startActivity(intent);
        } else {
            Intent intent = new Intent(getActivity(), SongSheetDetailActivity.class);
            intent.putExtra("id", addTo.getSongListId());
            startActivity(intent);
        }
    }
}
